import {
	TabSearchViewActionType,
	ITabSearchViewAbility,
	ITabSearchViewController,
	ITabSearchViewStore,
	ITabSearchViewControllerParams,
	IParam,
	ITabExpPanelAbility,
	MDCtrlActionType,
	SearchFormActionType,
	TabExpPanelActionType,
	IViewActionResult,
} from '@/core/interface';
import { deepCopy } from '@/core/utils';
import { MDViewController } from './md-view-controller';

/**
 * 分页搜索视图控制器
 *
 * @export
 * @class TabSearchViewController
 * @extends {ExpViewController<TabSearchViewActionType, ITabSearchViewStore, ITabSearchViewAbility>}
 * @implements {ITabSearchViewController<ITabSearchViewStore, ITabSearchViewAbility>}
 */
export class TabSearchViewController
	extends MDViewController<
		TabSearchViewActionType,
		ITabSearchViewStore,
		ITabSearchViewAbility
	>
	implements
		ITabSearchViewController<ITabSearchViewStore, ITabSearchViewAbility>
{
	/**
	 * @description 处理视图初始化
	 * @protected
	 * @param {ITabSearchViewControllerParams<TabSearchViewActionType, ITabSearchViewAbility>} params
	 * @memberof TabSearchViewController
	 */
	protected processViewInit(
		params: ITabSearchViewControllerParams<
			TabSearchViewActionType,
			ITabSearchViewAbility
		>
	) {
		super.processViewInit(params);
	}

	/**
	 * @description 获取分页导航面板能力
	 * @protected
	 * @param {ITabSearchViewControllerParams<TabSearchViewActionType, ITabSearchViewAbility>} params
	 * @memberof TabSearchViewController
	 */
	protected getMainCtrlAbility(): ITabExpPanelAbility | undefined {
		const tabExpPanel = this.model.ctrls.find(
			(ctrl: IParam) => ctrl.controlType === 'TABEXPPANEL'
		);
		if (tabExpPanel) {
			return this.getSubAbility<ITabExpPanelAbility>(tabExpPanel.name);
		}
	}

	/**
	 * 视图加载
	 *
	 * @param {IParam} [args]
	 * @return {*}  {Promise<IViewActionResult>}
	 * @memberof TabSearchViewController
	 */
	public load(args?: IParam): Promise<IViewActionResult> {
		const searchForm = this.getSearchFormAbility();
		if (searchForm) {
			const tempViewParams = deepCopy(this.store.viewParams);
			return searchForm.loadDraft(tempViewParams);
		}
		const tabExpPanel = this.getMainCtrlAbility();
		if (tabExpPanel) {
			return tabExpPanel.load();
		} else {
			return Promise.reject({
				ok: false,
				data: null,
				rowData: { status: 500 },
			});
		}
	}

	/**
	 * 快速分组值变化
	 *
	 * @param {IParam} item
	 * @memberof MDViewController
	 */
	public handleQuickGroupValueChange(item: IParam) {
		if (item) {
			this.queryParams.quickGroupData = item.data;
			const tabExpPanel = this.getMainCtrlAbility();
			if (tabExpPanel) {
				const tempViewParams = deepCopy(this.store.viewParams);
				tabExpPanel.load(tempViewParams);
			}
		}
	}

	/**
	 * 搜索
	 *
	 * @param {string} searchValue
	 * @memberof TabSearchViewController
	 */
	public search(searchValue: string) {
		this.queryParams.query = searchValue;
		const tabExpPanel = this.getMainCtrlAbility();
		if (tabExpPanel) {
			tabExpPanel.load({ query: searchValue, srfpagereset: true });
		}
	}

	/**
	 * 处理部件行为
	 *
	 * @param {string} name
	 * @param {(MDCtrlActionType | SearchFormActionType)} action
	 * @param {IParam[]} data
	 * @memberof TabSearchViewController
	 */
	public handleCtrlAction(
		name: string,
		action: MDCtrlActionType | SearchFormActionType | TabExpPanelActionType,
		data: IParam[]
	): void {
		const tabExpPanel = this.getMainCtrlAbility();
		if (tabExpPanel && name === tabExpPanel.name) {
			this.handleTabExpPanelAction(action as TabExpPanelActionType, data);
			return;
		}
		const searchForm = this.getSearchFormAbility();
		if (searchForm && name === searchForm.name) {
			this.handleSearchFormAction(action as SearchFormActionType, data);
			return;
		}
	}

	/**
	 * 处理分页导航面板行为
	 *
	 * @param {string} name
	 * @param {TabExpPanelActionType} action
	 * @param {IParam[]} data
	 * @memberof TabSearchViewController
	 */
	public handleTabExpPanelAction(
		action: TabExpPanelActionType,
		data: IParam[]
	) {
		if (action === 'tabClick') {
			this.loadTabExpPanelData();
		}
	}

	/**
	 * 处理搜索表单部件行为
	 *
	 * @param {SearchFormActionType} action
	 * @param {IParam[]} data
	 * @memberof TabSearchViewController
	 */
	protected handleSearchFormAction(
		action: SearchFormActionType,
		data: IParam[]
	) {
		if (action === 'load') {
			this.loadTabExpPanelData();
		}
		if (action === 'search') {
			this.loadTabExpPanelData();
		}
	}

	/**
	 * 加载TabExpPanel数据
	 *
	 * @memberof TabSearchViewController
	 */
	public loadTabExpPanelData() {
		const tabExpPanel = this.getMainCtrlAbility();
		const searchForm = this.getSearchFormAbility();
		const tempViewParams = deepCopy(this.store.viewParams);
		if (searchForm) {
			Object.assign(tempViewParams, searchForm.getData()[0]);
		}
		if (this.queryParams && Object.keys(this.queryParams).length > 0) {
			if (this.queryParams['quickGroupData']) {
				Object.assign(tempViewParams, this.queryParams['quickGroupData']);
			}
			if (this.queryParams['query'] && !this.model.expandSearchForm) {
				Object.assign(tempViewParams, { query: this.queryParams['query'] });
			}
		}
		if (tabExpPanel) {
			tabExpPanel.load(tempViewParams);
		}
	}
}
